package com.hos.operation.push.gtui;


import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.hos.net.res.push.PushRes;
import com.hos.utile.DLog;
import com.hos.utile.JsonUtile;
import com.hos.utile.StringUtile;

/**
 * 消息推送
 * Created by Administrator on 2018/8/24 0024.
 */
public abstract class PushManager {

    /* private String appId = "pj6cfxTl686ilCvfQhEgB5";
     private String appKey = "BJ56LEFVoL80UncU9pw4m1";
     private String masterSecret = "14d2Or8h097DVTMq9Z7OD8";*/
    private String url = "http://sdk.open.api.igexin.com/apiex.htm";


    abstract String getAppId();

    abstract String getAppKey();

    abstract String getMasterSecret();

    /***
     *
     * @param pushId 推送id
     * @param consultId 咨询id
     * @param msg 咨询消息
     * @param msgType 消息类型  TEXT文字 PIC 图片
     * @param isTagDoc true 推送给医生 false 推送给患者
     */
    public void onConsultChat(String userId, String pushId, String consultId, String msg, String msgType, boolean isTagDoc) {
        if (StringUtile.isEmpty(pushId)) {
            return;
        }
        PushRes res = new PushRes();
        res.pushType = "CON_CHAT";
        res.title = "新回复";
        res.userId = userId;
        res.id = consultId;
        if ("PIC".equals(msgType)) {
            msg = "[图片]";
        }
        if (msg.length() > 20) {
            msg = msg.substring(0, 20);
        }
        res.msg = msg;
        String pushMsg = JsonUtile.obj2Json(res);
        onPush(pushId, pushMsg);
        DLog.e("推送：pushid" + pushId + " 消息" + msg);
    }

    //咨询结束
    public void onConsultEnd(String userId, String pushId, String consultId) {
        if (StringUtile.isEmpty(pushId)) {
            return;
        }
        PushRes res = new PushRes();
        res.pushType = "CON_END";
        res.title = "咨询结束";
        res.userId = userId;
        res.id = consultId;
        res.msg = "医生已结束您的咨询";
        String pushMsg = JsonUtile.obj2Json(res);
        onPush(pushId, pushMsg);
        DLog.e("推送：pushid" + pushId);
    }

    public void onConsultAdd(String userId, String pushId, String consultId, String msg, String msgType) {
        if (StringUtile.isEmpty(pushId)) {
            return;
        }
        PushRes res = new PushRes();
        res.pushType = "CON_ADD";
        res.title = "新的咨询";
        res.userId = userId;
        res.id = consultId;
        if (msg == null) {
            msg = "";
        }
        if (msg.length() > 20) {
            msg = msg.substring(0, 20);
        }
        res.msg = msg;
        String pushMsg = JsonUtile.obj2Json(res);
        onPush(pushId, pushMsg);
        DLog.e("推送：pushid" + pushId + " 消息" + msg);
    }

    protected void onPush(String pushId, String pushMsg) {
        onPush(pushId, pushMsg, null, null);

    }

    protected void onPush(String pushId, String pushMsg, Notify notify, APNPayload iosPayload) {
        //检查参数
        try {
            url = null;
            IGtPush gtPush = new IGtPush(url, getAppKey(), getMasterSecret());
            TransmissionTemplate template = getTemplate(pushMsg);
            //设置离线推送
            if (notify != null) {
                template.set3rdNotifyInfo(notify);
            }
            //设置ios离线推送
            if (iosPayload != null) {
                template.setAPNInfo(iosPayload);
            }
            //
            SingleMessage sm = new SingleMessage();
            sm.setOffline(true);
            // 离线有效时间，单位为毫秒，可选(设置似乎更有效)
            sm.setOfflineExpireTime(24 * 3600 * 1000);
            sm.setData(template);
            //可选。判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            sm.setPushNetWorkType(0);
            IPushResult ret = gtPush.pushMessageToSingle(sm, getTarget(pushId));
            DLog.e("推送成功:" + JsonUtile.obj2Json(ret));
        } catch (Exception e) {
            DLog.e("推送失败；" + e.getMessage());
        }

    }

    /**
     * 消息发送目标
     *
     * @param pushId
     * @return
     */
    private Target getTarget(String pushId) {
        Target target = new Target();
        target.setAppId(getAppId());
        target.setClientId(pushId);
        return target;
    }

    //透传模板动作内容
    private TransmissionTemplate getTemplate(String pushMsg) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(getAppId());
        template.setAppkey(getAppKey());
        //应用启动类型，1：强制应用启动 2：等待应用启动
        template.setTransmissionType(2);
        //透传内容
        template.setTransmissionContent(pushMsg);
        //
        //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，
        //消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
        // String begin = "2015-03-06 14:36:10";
        // String end = "2015-03-06 14:46:20";
        //template.setDuration(begin, end);
        return template;
    }

    /**
     * @param title 消息名称
     * @param body  消息内容
     * @return
     */
    protected APNPayload setIOS(String title, String body) {
        //iOS 推送
        APNPayload payload = new APNPayload();
        payload.setAutoBadge("+1");
        payload.setContentAvailable(0);
        payload.setSound("sound.caf");
        //payload.setApnsCollapseId(apnsCollapseId);
        //payload.setContentAvailable();	//推送直接带有透传数据
        //增加自定义的数据,Key-Value形式
        //payload.addCustomMsg("payload", "payload");
        payload.setAlertMsg(getDictionaryAlertMsg(title, body));
        return payload;


    }

    private APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String body) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setTitle(title);
        alertMsg.setBody(body);
        return alertMsg;
    }

}
